<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\NewsCategoryModel;
use app\common\model\NewsContentModel;
use app\common\model\NewsModel;
use think\console\Input;
use think\facade\Session;
use dh2y\qrcode\QRcode;

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
class News extends Common
{
    public function getList()
    {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $keyword = input('keyword');
        $category_id = input('category_id');

        if(!empty($keyword)){
            $this->comMap[] = ['title|id','like','%'.$keyword.'%'];
        }
        if(!empty($category_id)){
            $this->comMap[] = ['category_id','=',$category_id];
        }

        $list = NewsModel::pageList($this->comMap);
        return json(['code'=>0,'msg'=>'success','data'=>$list,'map'=>$this->comMap]);
    }

    public function del(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $id = $this->request->param('ids');
        NewsModel::destroy($id);
        return json(['code'=>0,'msg'=>'删除成功']);
    }

	public function recommending(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$ids = $this->request->param('ids');
		$status = $this->request->param('status',1);
		NewsModel::recommending($ids,$status);
		return json(['code'=>0,'msg'=>'操作成功']);
	}

	public function topping(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
		$ids = $this->request->param('ids');
		$status = $this->request->param('status',1);
		NewsModel::topping($ids,$status);
		return json(['code'=>0,'msg'=>'操作成功']);
	}


    public function add(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $data = $this->request->param('data');
        if(empty($data['id'])){     //判断文章是否存在ID  存在ID即为更新 否则为新增
            if (!empty($data['id_domain'])) {   //新增文章需要判断文章个性地址是否重复及合规
                if(!preg_match('/^[A-Za-z0-9\_]+$/u',$data['id_domain'])){
                    return json(['code'=>1010, 'msg'=>'文章个性地址只能是字母或数字的组合']);
                }
                $item = NewsModel::where('id_domain',$data['id_domain'])->find();
                if ($item) {
                    return json(['code'=>1010, 'msg'=>'文章个性地址已经存在']);
                }
            }
        }
        if(!empty($data['create_time'])){
            $data['create_time'] = strtotime($data['create_time']);
        }
        if(!empty($data['id'])){
            if(empty($data['news_image'])){     //判断当前ID所属文章是否有二维码  新增文章不创建二维码 必须通过编辑文章创建
                $code = new QRcode();
                $news_url = '/show/'.$data['id'].'.html';   //二维码url地址
                $newsImage = $code->png($news_url,false, 10)->getPath();
                $data['news_image'] = $newsImage;
            }
            NewsModel::update($data);
            NewsContentModel::updateByMain($data['id'],$data['content']);
        }else if(!empty($data['thumb'])){    //判断文章是否有上传图片
            unset($data['id']);
            $data['admin_id'] = session('admin_id');
            $Info = NewsModel::create($data);
	        NewsContentModel::updateByMain($Info->id,$data['content']);
        }else{
            unset($data['id']);
            $data['admin_id'] = session('admin_id');
            $data['thumb'] = '/storage/news/article/'.rand(1,99).'.jpg';    //如未上传图片则添加随机图片
            $Info = NewsModel::create($data);
	        NewsContentModel::updateByMain($Info->id,$data['content']);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }
    
    // 管理文章
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $userList = NewsModel::order('create_time DESC')->select();
        if ($this->request->isPost()) {
            $id         = input('post.id');
            $value      = input('post.value');
            $action     = input('post.action');
            $status     = input('post.status');
            $category   = input('post.category');
            if($value == 'true'){
                $value = '1';
            }elseif($value == 'false'){
                $value = '0';
            }
            // 更新留言信息
            if($category == 'status'){
                NewsModel::where('id', $id)->update([$status => $value]);
            }else if($category == 'id_domain'){
                if (!empty($value)) {
                    if(!preg_match('/^[A-Za-z0-9\_]+$/u',$value)){
                        return json(['code'=>1010, 'msg'=>'文章个性地址只能是字母或数字的组合']);
                    }
                    $item = NewsModel::where('id_domain',$value)->find();
                    if ($item) {
                        return json(['code'=>1010, 'msg'=>'文章个性地址已经存在']);
                    }
                }
                NewsModel::update([$category => $value,'id'=>$id]);
            }else{
                NewsModel::update([$category => $value,'id'=>$id]);
            }
            return json(['code'=>0,'msg'=>'操作成功']);
        }
        $this->getSide();
        return $this->fetch();
    }


    /**
     * 获取分类列表
     */
    public function getCategoryList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $lang = input('lang');
        $NewsCategory =  new NewsCategoryModel();
        $list = $NewsCategory->getFullList();
        foreach ($list as $key=>$val){
			if($lang=='en'){
				$list[$key]['name'] = $val['ename'];
			}
        }
        return json(['code'=>0,'msg'=>'操作成功','data'=>$list]);
    }

    /**
     * 添加分类
     */
    public function categoryAdd(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $data = $this->request->param();
        if(empty($data['id'])){
            unset($data['id']);
            NewsCategoryModel::create($data);
        }else{
            NewsCategoryModel::update($data);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }

    /**
     * 删除分类
     */
    public function categoryDel(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $ids = $this->request->param('ids');
        NewsCategoryModel::destroy($ids);
        return json(['code'=>0,'msg'=>'删除成功']);
    }


    public function changeStatus(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
	    $id = $this->request->param('id');
	    $info = NewsModel::find($id);
	    if($info->status==1){
		    $info->status=0;
		    $msg = '已禁用';
	    }else{
		    $info->status=1;
		    $msg = '已启用';
	    }
	    $info->save();
	    return json(['code'=>0,'msg'=>$msg]);
    }

}
